// reactivty
// 实现add[1][2][3]+ 100

let effectfunc = null;
class Dep {
    constructor(){
        this.list = []
    }
    on(){
        if(!effectfunc) return;
        this.list.push(effectfunc)
    }
    emit(){
        this.list.forEach(effectfunc => effectfunc())
    }
}

let targetMap = new Map();
function getDep(target, key){
 
    let depMap = targetMap.get(target)
    if(!depMap){
        depMap = new Map()
        targetMap.set(target, depMap)
    }

    let dep = depMap.get(key)
    if(!dep){
        dep = new Dep()
        depMap.set(key, dep)
    }

    return dep
}

function reactivty(target){
    return new Proxy(target, {
        get(target, key, reciver){
            let dep = getDep(target, key)
            dep.on()
            return Reflect.get(target, key, reciver)
        },
        set(target, key, val, reciver){
            let dep = getDep(target, key)
            let result = Reflect.set(target, key, val, reciver)
            dep.emit()
            return result
        }
    })
}

function effect(effect){
    effectfunc = effect;
    effectfunc();
    effectfunc = null
}

// 测试
let obj = reactivty({count: 0})

effect(() => {
    console.log(obj.count)
})

obj.count++;




const add = function createProxy(values=[]){
    return new Proxy({}, {
        get(target, key, reciver){
            console.log(key)
            if(key === Symbol.toPrimitive){
                return () =>  values.reduce((pre, next) => pre + next, 0)
            }
            return createProxy([...values, +key])
        }
    })
}

console.log(add()[1][2] + 3)